#!/bin/bash

version=1.0.0
echo "run EnvInstall 1.0.0..."

# 系统更新, 安装驱动, 依赖软件及库
echo Y | sudo apt update
echo Y | sudo apt upgrade
# echo Y | sudo ubuntu-drivers autoinstall
echo Y | sudo apt install openssh-server
echo Y | sudo apt install curl vim net-tools
echo Y | sudo apt install libopencv-dev
echo Y | sudo apt install python3-tk python3-dev
echo Y | sudo apt install python3-pip
echo Y | sudo apt install exfat-utils ntfs-3g
echo Y | sudo apt install gnome-tweak-tool
echo Y | sudo apt install libzbar0

# 开发编译环境, 用户部署不用装
# echo Y | sudo apt install exuberant-ctags gcc make cmake

# 安装python
cd /usr/bin
sudo unlink python
sudo ln -s python3 python
cd -

# 安装cuda
echo Y | sudo apt install nvidia-cuda-toolkit

# Chinese language
echo Y | sudo apt install fcitx fcitx-table-wubi fcitx-pinyin

# 注意: 需要在 bios 中关闭 secure boot 选项, 以启用 nvidia 显卡

# 测试显卡是否生效
#nvidia-smi

# 安装数据库
echo Y | sudo apt install mysql-server
echo Y | sudo apt install mysql-client
echo Y | sudo apt install libmysqlclient-dev

# mysql default account
#sudo cat /etc/mysql/debian.cnf

# 修改数据库配置, 支持远程连接
mysqld_cfg=/etc/mysql/mysql.conf.d/mysqld.cnf
if [ -f $mysqld_cfg ]; then
    for s in bind-address mysqlx-bind-address; do
        sudo sed -i "s/^$s/#$s/" $mysqld_cfg
    done
fi

# for UI
echo Y | sudo apt install wkhtmltopdf

# switch pip resource
mkdir -p ~/.pip
echo "[global]" > ~/.pip/pip.conf
#echo "index-url = http://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf
#echo "trusted-host = mirrors.aliyun.com" >> ~/.pip/pip.conf
echo "index-url = https://pypi.mirrors.ustc.edu.cn/simple" >> ~/.pip/pip.conf
echo "trusted-host = pypi.mirrors.ustc.edu.cn" >> ~/.pip/pip.conf
pip3 install --upgrade pip

if [ -f Dependent/requirements.txt ]; then
    pip3 install --user Cython=='0.29.30'
    pip3 install --user -r Dependent/requirements.txt
else
    pip3 install --user cython
    pip3 install --user pymysql
    pip3 install --user pyserial
    pip3 install --user Pillow=='9.4.0'
    pip3 install --user PyQt5=='5.13.2'
    pip3 install --user numpy=='1.23.5'
    pip3 install --user matplotlib=='3.5.2'
    pip3 install --user opencv-python=='4.5.5.64'
    pip3 install --user scikit-build=='0.15.0'
    pip3 install --user scikit-learn=='0.21.3'
    pip3 install --user pyperclip
    pip3 install --user pdf2image
    pip3 install --user pdfkit
    pip3 install --user pyzbar
    pip3 install --user xlwt
    pip3 install --user xlrd
    pip3 install --user xlutils
    pip3 install --user yacs
    pip3 install --user jwt
fi

# for torch
# 当显卡是3060时, 需要安装特定版本的torch, 否则无法运行
if [ -f Dependent/torch-1.9.1+cu111-cp38-cp38-linux_x86_64.whl ] && [ -f Dependent/torchvision-0.10.1+cu111-cp38-cp38-linux_x86_64.whl ]; then
    pip3 install --user Dependent/torch-1.9.1+cu111-cp38-cp38-linux_x86_64.whl
    pip3 install --user Dependent/torchvision-0.10.1+cu111-cp38-cp38-linux_x86_64.whl
    # pip3 install --user torchnet
else
    pip3 install --user torch torchvision
    # pip3 install --user torchnet
fi

# 加密狗驱动
cd ./Runtime
sudo dpkg -i aksusbd_8.31-1_amd64.deb
cd -

# 准备目录
mkdir ~/prj
mkdir ~/prj/ZJ
mkdir ~/prj/ZJ/bin
sudo mkdir /mnt/dat1
sudo chown manager:manager /mnt/dat1
# ln -s /mnt/dat1/dat ~/prj/ZJ/dat1

# 更方便开发的环境
echo Y | sudo apt install git
cd ./Software/vim
bash ./install.sh
cd -
cd ./Software/zsh
bash ./install.sh
cd -

# 允许生成core文件
if [ -f /etc/sysctl.conf ]; then
    echo "kernel.core_pattern=%e.core.%p" | sudo tee -a /etc/sysctl.conf
fi
if [ -f /etc/default/apport ]; then
    sudo sed -i 's/enabled=.*/enabled=0/' /etc/default/apport
fi
# ulimit -c unlimited

# test torch
#python >> import torch
#python >> import torchvision
#python >> print(torch.cuda.is_available())

#===================== 关闭 http 模块自带的输出信息 =======================bgn
#$ python3
#>>> import http.server
#>>> http.server.__file__
#'/usr/lib/python3.7/http/server.py'
#>>> exit()
#
#$ sudo vim /usr/lib/python3.7/http/server.py
#＃ 找到 log_message 函数, 关闭 sys.stderr.write 输出, 再添加 pass 新行, 如下:
#        """
#        sys.stderr.write("%s - - [%s] %s\n" %
#                         (self.address_string(),
#                          self.log_date_time_string(),
#                          format%args))
#        """
#        pass
#===================== 关闭 http 模块自带的输出信息 =======================end
#
#
#===================== 配置硬盘自动挂载 ===================================bgn
## 查看磁盘信息
#sudo blkid
#
## 编辑自动挂载配置文件
##     UUID 的取值即为 blkid 中显示的 UUID
##     /media/manager/dat 为自动挂载的位置
#sudo vim /etc/fstab
#    UUID=11263962-9715-473f-9421-0b604e895aaa /media/manager/dat ext4 defaults 0 1
#
## 重启以使配置生效
#sudo reboot
#===================== 配置硬盘自动挂载 ===================================end
#
#
#===================== 解决 mysql 无法远程访问的问题 ======================bgn
## 注意: 根据实际的 mysql 帐号修改下面的 root 及 rootpswd
#mysql > GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' identified by 'rootswd';
#mysql > flush privileges;
#
#vim /etc/mysql/mysql.conf.d/mysqld.cnf
#注释以下行:
#    bind-address		= 127.0.0.1
#sudo /etc/init.d/mysql restart
#===================== 解决 mysql 无法远程访问的问题 ======================end
#
#=============== 解决关机时 mysql 无法停止的问题 ==========================bgn
#sudo chgrp mysql /etc/mysql/debian.cnf
#sudo chmod 640 /etc/mysql/debian.cnf
#sudo cp /lib/systemd/system/mysql.service /etc/systemd/system/
#sudo chmod 755 /etc/systemd/system/mysql.service
#sudo vim /etc/systemd/system/mysql.service
#    # add following code in [Service]
#    ExecStop=/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf shutdown
#sudo systemctl daemon-reload
#=============== 解决关机时 mysql 无法停止的问题 ==========================end
#
#=============== libcudnn.so.7.1.4 ========================================bgn
#cd ~/prj/ZJ/bin/Diagnose
#sudo cp ./libcudnn.so.7.1.4 /lib/x86_64-linux-gnu/
#cd /lib/x86_64-linux-gnu/
#sudo ln libcudnn.so.7.1.4 ./libcudnn.so.7.1 -s
#sudo ln libcudnn.so.7.1 ./libcudnn.so.7 -s
#=============== libcudnn.so.7.1.4 ========================================end
#
#=============== for cuda.10.1 ============================================bgn
#cd ~/prj/ZJ/bin/Diagnose
#mv zjNet zjNet_cuda.9.1
#ln zjNet zjNet_cuda.10.1 zjNet -s
#
#cd ~/prj/ZJ/bin/Diagnose/maskrcnn_benchmark
#mv _C.cpython-37m-x86_64-linux-gnu.so _C.cpython-37m-x86_64-linux-gnu.so_cuda.9.1
#ln _C.cpython-37m-x86_64-linux-gnu.so_cuda.10.1 _C.cpython-37m-x86_64-linux-gnu.so -s
#=============== for cuda.10.1 ============================================end
#
## try to use torch 1.2.0, but not be ready yet
## pip3 install torchvision=='0.4.0' torch=='1.2.0' torchnet=='0.0.4'
